<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\LoginRequest;
use App\Repositories\UserRepository;
use App\Utils\Auth;
use App\Utils\Output;
use Illuminate\Http\Request;

class PublicController extends BaseController
{
    public function __construct()
    {
        $this->set('public');
    }

    function login(Request $request)
    {
        $uid = Auth::userId();
        $request->session()->flush();
        return $this->view('login');
    }

    public function loging(LoginRequest $request, UserRepository $repository)
    {
        $result = $repository->login(trim($request->get('username')), $request->get('password'));
        if ($result->isError()) {
            return $result;
        }
        $request->session()->put(config('app.adminUserSession'), $result->data);
        Auth::setMenus();
        $url = $request->get('jurl');
        $result = Output::data(['jurl' => urldecode($url ? $url : url('/index'))]);
        $result->msg = "登录成功";
        return $result;
    }

    function icon(){
        return $this->view('icon');
    }
}
